package com.example.websocketclusterspringbootstarter.utils;

import com.example.websocketclusterspringbootstarter.exception.WsClusterException;
import com.example.websocketclusterspringbootstarter.handler.EventHandler;

import java.util.concurrent.ConcurrentHashMap;

public class EventHandlerUtil {

    private EventHandlerUtil() {
    }

    private static final ConcurrentHashMap<String, EventHandler> MAP = new ConcurrentHashMap<>();

    public static void addHandler(String name, EventHandler eventHandler) {
        MAP.put(name, eventHandler);
    }

    public static ConcurrentHashMap<String, EventHandler> allHandler() {
        return MAP;
    }


    /**
     * 根据handler名称获取 handler
     *
     * @param handlerName 事件处理器名称
     * @return handlerName对应的处理器
     */
    public static EventHandler getHandler(String handlerName) {
        EventHandler eventHandler = MAP.get(handlerName);
        if (null == eventHandler) {
            throw new WsClusterException(handlerName + " eventHandler not exist");
        }
        return eventHandler;
    }
}
